package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbPlan;
import com.ruoyi.wb.dto.WbClassifyYearDto;
import com.ruoyi.wb.dto.WbPlanAuditDto;
import com.ruoyi.wb.dto.WbPlanContentDto;
import com.ruoyi.wb.dto.WbPlanDto;
import com.ruoyi.wb.enmus.PlanAuditStatusEnums;
import com.ruoyi.wb.enmus.PlanExecuteStatusEnums;
import com.ruoyi.wb.enmus.PlanTypeEnums;
import com.ruoyi.wb.service.IWbPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 维保计划Controller
 *
 * @author ruoyi
 * @date 2024-10-14
 */
@Api(tags="-app端-维保计划信息管理")
@RestController
@RequestMapping("/wb/app/wbPlan")
public class WbAppPlanController extends BaseController {

    @Autowired
    private IWbPlanService wbPlanService;

    @ApiOperation("查询当前登录用户维保信息")
    @GetMapping("/selectUserMaintainInfoPage")
    public TableDataInfo selectUserMaintainInfoPage( WbPlan wbPlan) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.selectUserMaintainInfoPage(wbPlan,user.getUserId());
        return getDataTable(list);
    }

    @ApiOperation("上传凭条")
    @PostMapping("/entrySlip")
    public AjaxResult entrySlip(@RequestBody WbPlan wbPlan) {
        wbPlan.setExecStatus(PlanExecuteStatusEnums.SUBMITTED.getStatus());
        wbPlan.setUpdateTime(DateUtils.getNowDate());
        wbPlan.setAuditStatus(PlanAuditStatusEnums.PENDING_REVIEW.getStatus());
        return toAjax(wbPlanService.updateWbPlan(wbPlan));
    }

    @ApiOperation("我已到达")
    @GetMapping("/arrive")
    public AjaxResult arrive(@RequestParam("planId") Long planId) {
        WbPlan wbPlan = new WbPlan();
        wbPlan.setId(planId);
        wbPlan.setExecStatus(PlanExecuteStatusEnums.HAVE_IN_HAND.getStatus());
        wbPlan.setExecStartTime(new Date());
        return toAjax(wbPlanService.updateWbPlan(wbPlan));
    }

    @ApiOperation("修改时间")
    @PostMapping("/updateTime")
    public AjaxResult edit(@RequestBody WbPlan wbPlan) {
        return toAjax(wbPlanService.updateWbPlan(wbPlan));
    }

    @ApiOperation("上传任务表单信息查询")
    @GetMapping("/getJobInfoByPlanId")
    public AjaxResult getJobInfoByPlanId(@RequestParam("planId") Long planId) {
        return success(wbPlanService.getJobInfoByPlanId(planId));
    }

    @ApiOperation("提交任务")
    @PostMapping("/submitJob")
    public AjaxResult submitJob(@RequestBody List<WbPlanContentDto>dtos) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return success(wbPlanService.submitJob(dtos,user.getUserId()));
    }

    @ApiOperation("我要年检")
    @GetMapping("/annual")
    public TableDataInfo annual(@RequestParam("key") String key) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.examineJob(user.getUserId(),key, PlanTypeEnums.YEAR.getType());
        return getDataTable(list);
    }

    @ApiOperation("我要年检任务数")
    @GetMapping("/annualCount")
    public AjaxResult annualCount() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.examineJob(user.getUserId(),null, PlanTypeEnums.YEAR.getType());
        return success(list.size());
    }



    @ApiOperation("我要月检")
    @GetMapping("/monthly")
    public TableDataInfo monthly(@RequestParam("key") String key) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.examineJob(user.getUserId(),key, PlanTypeEnums.MONTHLY.getType());
        return getDataTable(list);
    }

    @ApiOperation("我要月检任务数")
    @GetMapping("/monthlyCount")
    public AjaxResult monthlyCount() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.examineJob(user.getUserId(),null, PlanTypeEnums.MONTHLY.getType());
        return success(list.size());
    }


    @ApiOperation("批量选择任务为今日任务")
    @PostMapping("/todayJob")
    public AjaxResult todayJob(@RequestBody List<Long> planIds) {
        return toAjax(wbPlanService.todayJob(planIds));
    }

    @ApiOperation("数据统计")
    @GetMapping("/dataStatistics")
    public AjaxResult dataStatistics() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return success(wbPlanService.dataStatistics(user.getUserId()));
    }

    @ApiOperation("维保计划审核通过")
    @PostMapping("/planAudit")
    public AjaxResult planAudit(@RequestBody WbPlanAuditDto dto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        dto.setUserId(user.getUserId());
        return toAjax(wbPlanService.planAudit(dto));
    }

    @ApiOperation("维保计划驳回")
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody WbPlanAuditDto dto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        dto.setUserId(user.getUserId());
        return toAjax(wbPlanService.reject(dto));
    }

    @ApiOperation("下载后更改计划为已完成")
    @GetMapping("/accomplish")
    public AjaxResult accomplish(@RequestParam("planId") Long planId) {
        WbPlan wbPlan = new WbPlan();
        wbPlan.setId(planId);
        wbPlan.setExecStatus(PlanExecuteStatusEnums.COMPLETED.getStatus());
        wbPlanService.updateWbPlan(wbPlan);
        return success();
    }

    @ApiOperation("今日维保")
    @GetMapping("/todayPlan")
    public TableDataInfo todayPlan(WbPlanDto dto) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.todayPlan(dto.getExecStatus(), user.getUserId());
        return getDataTable(list);
    }
    @ApiOperation("今日维保任务数")
    @GetMapping("/todayPlanCount")
    public AjaxResult todayPlanCount() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.todayPlan(PlanExecuteStatusEnums.TO_BEGIN.getStatus(), user.getUserId());
        List<WbPlan> list2 = wbPlanService.todayPlan(PlanExecuteStatusEnums.HAVE_IN_HAND.getStatus(), user.getUserId());
        return success(list.size()+list2.size());
    }




    @ApiOperation("获取维保计划详情")
    @GetMapping("/getPlanInfoById")
    public AjaxResult getPlanInfo(@RequestParam("planId") Long planId) {
        return success(wbPlanService.getPlanInfo(planId));
    }

    @ApiOperation("获取维保计划审核记录")
    @GetMapping("/getPlanAuditLog")
    public AjaxResult getPlanAuditLog(@RequestParam("planId") Long planId) {
        return success(wbPlanService.getPlanAuditLog(planId));
    }

    @ApiOperation("查询当前登录用户审核信息")
    @GetMapping("/selectUserAuditPage")
    public TableDataInfo selectUserAuditPage(@RequestParam("execStatus") Integer execStatus) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<WbPlan> list = wbPlanService.selectUserAuditPage(execStatus,user.getUserId());
        return getDataTable(list);
    }

    @ApiOperation("查询当前用户维保审核任务数")
    @GetMapping("/selectUserAuditPageCount")
    public AjaxResult selectUserAuditPageCount() {
       int i= wbPlanService.selectUserAuditPageCount();
        return success(i);
    }


    @ApiOperation("统计维保审核数量")
    @GetMapping("/statisticsMaintenanceAuditNumber")
    public AjaxResult statisticsMaintenanceAuditNumber() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return success(wbPlanService.statisticsMaintenanceAuditNumber(user.getUserId()));
    }

    @ApiOperation("查询年检类型维保任务")
    @GetMapping("/getYearPlanJob")
    public TableDataInfo getYearPlanJob(WbPlan wbPlan) {
        startPage();
        wbPlan.setOperaterId(getUserId());
        wbPlan.setPlanType(PlanTypeEnums.YEAR.getType());
        wbPlan.setExecStatus(PlanExecuteStatusEnums.TO_BEGIN.getStatus());
        return getDataTable(wbPlanService.selectWbPlanList(wbPlan));
    }

    //预览月检
    @ApiOperation("预览月检")
    @GetMapping("/previewMonthly/{planId}")
    public AjaxResult previewMonthly(@PathVariable("planId") Long planId) {

        return success(wbPlanService.previewMonthly(planId));
    }

    //预览年检
    @ApiOperation("预览年检")
    @GetMapping("/previewYearly/{planId}")
    public AjaxResult previewYearly(@PathVariable("planId") Long planId) {
        return success(wbPlanService.previewYearly(planId));
    }

    @ApiOperation("新增个人年检目录")
    @PostMapping("/saveCatalog")
    public AjaxResult saveCatalog(@RequestBody List<WbClassifyYearDto> dtoList) {
        return success(wbPlanService.saveCatalog(dtoList));
    }
}